//
//  assetsOViewController.m
//  ChinaA
//
//  Created by Mxionlly on 2018/11/5.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "assetsOViewController.h"
#import "AssetsTableViewCell.h"
#import <WebKit/WebKit.h>

@interface assetsOViewController ()<UITableViewDelegate,UITableViewDataSource,WKUIDelegate,UIWebViewDelegate>
@property (nonatomic,strong)    UITableView *tableView;
@property (nonatomic,strong)    NSArray     *cellArray;
@property (nonatomic,strong)    UIWebView *webView;
@property (nonatomic,strong)    NSMutableArray *TCArray;
@end

@implementation assetsOViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.webView = [UIWebView new];
    self.webView.delegate = self;
    self.TCArray = [NSMutableArray new];
    [self.view addSubview:self.tableView];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadWebView];
}

-(void)loadWebView{
    [self.TCArray removeAllObjects];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.zb.cn/data/v1/ticker?market=btc_usdt"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5.0]];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // Disable user selection
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
    // Disable callout
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
    
    NSString *body =  [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerText"];
    
    [self.TCArray addObject:[[[Check dictionaryWithJsonString:body] valueForKey:@"ticker"] valueForKey:@"last"]];
    if (self.TCArray.count == 1) {
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.zb.cn/data/v1/ticker?market=btc_qc"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5.0]];
    }else if (self.TCArray.count == 2){
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.zb.cn/data/v1/ticker?market=eth_usdt"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5.0]];
    }else if (self.TCArray.count == 3){
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.zb.cn/data/v1/ticker?market=eth_qc"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5.0]];
    }else{
        [self.tableView reloadData];
    }
}


#pragma mark - loadtableview
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-246) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor  = COLOR_Background;
        _tableView.userInteractionEnabled = YES;
    }
    return _tableView;
}

#pragma mark - UITableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.TCArray.count == 4) {
        return 2;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.00001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AssetsTableViewCell *cell = [AssetsTableViewCell cellWithTableView:tableView];
    if (indexPath.row == 0) {
        cell.lbtitle.text = @"BTC";
        if (self.TCArray.count == 4) {
            cell.iconImageView.image = [UIImage imageNamed:@"icon_BTC"];
            cell.label.text = @"BTC/USD:";
            cell.label1.text = @"BTC/CNY:";
            cell.lbcontent.text = self.TCArray[0];
            cell.lbcontent1.text = self.TCArray[1];
        }
    }else{
        cell.lbtitle.text = @"ETH";
        if (self.TCArray.count == 4) {
            cell.iconImageView.image = [UIImage imageNamed:@"icon_ETH"];
            cell.label.text = @"ETH/USD:";
            cell.label1.text = @"ETH/CNY:";
            cell.lbcontent.text = self.TCArray[2];
            cell.lbcontent1.text = self.TCArray[3];
        }
    }
    return cell;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
